<template>
    <div>
        <a href='/AddView'>新增</a><br>
         姓名<input type="text" v-model="qag.name">
        状态<select v-model="qag.zhun">
            <option value="">请选择</option>
            <option value="启用">启用</option>
            <option value="禁用">禁用</option>
        </select>
        <input type="button" value="查询" @click="Show">
        <input type="button" value="批量修改" @click="Qunt">
        <table>
            <thead>
                <tr>
                    <td>
                        <input type="checkbox" v-model="select" @change="selectall">
                    </td>
                    <td>姓名</td>
                    <td>图片</td>
                    <td>时间</td>
                    <td>状态</td>
                    <td>描述</td>
                    <td>操作</td>
                </tr>
            </thead>
            <tbody>
                <tr v-for="m in data">
                    <td>
                        <input type="checkbox" v-model="idslit" :value="m.id">
                    </td>
                    <td>{{ m. name}}</td>
                    <td><img :src="'https://localhost:7087'+m.img" alt="" style="width: 60px;height: 60px;"></td>
                    <td>{{ m. seatime}}</td>
                    <td>{{ m.zhun }}</td>
                    <td>{{ m.ment }}</td>
                    <td>
                        <input type="button" value="修改" @click="xin(m.id)">
                        <input type="button" value="删除" @click="Del(m.id)">
                    </td>
                </tr>
            </tbody>
        </table>
        <input type="button" value="首页" @click="Fent(1)">
        <input type="button" value="上一页" @click="Fent(2)">
        <input type="button" value="上一页" @click="Fent(3)">
        <input type="button" value="尾页" @click="Fent(4)">
        一共{{ page.pcount }}页，一共{{ page.count }}条，每页{{ qag.parsize }}条，当前的{{ qag.pardex }}页
        <select v-model="qag.parsize" @change="Setsize">
            <option>2</option>
            <option>4</option>
            <option>6</option>
        </select>
        <input type="text" v-model="qag.pardex">
        <input type="button" value="跳转" @click="setdex">
    </div>
</template>

<script setup lang="ts">
import {ref ,onMounted} from 'vue';
import axios from 'axios';
import { useRouter } from 'vue-router';
import { uselogerStore } from '@/stores/loger';

const user=uselogerStore();

const rount=useRouter()

const idslit=ref([0])

const select=ref(false)

const Qunt=()=>{
    var ids=idslit.value.toString();
    axios.get("https://localhost:7087/api/User/UpdateRang",{params:{idstr:ids}}).then(res=>{
        if(res.data>0){
            alert('批量修改成功')
            Show()
        }
    })
}

const selectall=()=>{
    if(select.value){
        for(var i=0;i<data.value.length;i++){
            idslit.value.push(data.value[i].id);
        }
    }
    else{
        idslit.value=[0]
    }
}

const Del=(vid:any)=>{
    if(!confirm('确认删除吗？')){
        return;
    }
    axios.delete("https://localhost:7087/api/User/Delete",{params:{id:vid}}).then(res=>{
        if(res.data>0){
            alert('删除成功')
            Show()
        }
    })
}

const xin=(vid:any)=>{
    rount.push({path:'XinView',query:{id:vid}})
}


onMounted(() => {
   Show()  

    var state=user.user.islogin;
    if(!state)
    {
        rount.push({path:'/LoginView'})
    }
})

const data=ref([{
  "id": 0,
  "name": "",
  "pwd": "",
  "img": "",
  "seatime": "",
  "zhun": "",
  "ment": ""
}])

const setdex=()=>{
    if(qag.value.pardex>1&&qag.value.pardex<page.value.pcount){
        Show()
    }
}

const Setsize=()=>{
    qag.value.pardex=1;
    Show()
}

const Fent=(val:any)=>{
    if(val==1){
        qag.value.pardex=1;
        Show()
    }
    if(val==2){
        if(qag.value.pardex>1){
            qag.value.pardex-=1;
            Show()
        }
    }
    if(val==3){
        if(qag.value.pardex<page.value.pcount){
            qag.value.pardex+=1;
            Show()
        }
    }
    if(val==4){
        qag.value.pardex=page.value.pcount;
        Show()
    }
}

const qag=ref({
    pardex:1,
    parsize:2,
    name:"",
    zhun:""
})

const page=ref({
    count:0,
    pcount:0
})


const Show=()=>{
    axios.get("https://localhost:7087/api/User/Show",{params:qag.value}).then(res=>{
        data.value=res.data.data;
        page.value.count=res.data.count;
        page.value.pcount=Math.ceil(page.value.count/qag.value.parsize);
    })
}


</script>

<style scoped>

</style>